/*-------------------------------------------------------------------------------------
  CLASS : qDojo.core.StringEx
  ==================================
  - Some string functions.  
  - Intended as extension to dojo.String     
  -------------------------------------------------------------------------------------*/
dojo.provide("qDojo.core.StringEx");

/* -------------------------------------------------------------------------------------
	Change string to camel case: low tide/Low Tide -> lowTide
   ========================================================= */   	
qDojo.core.toCamelCase = function(str)
{
	str = str.toLowerCase();
	if(str.indexOf("_")>-1)
		str = str.replace(/_/g,' ');
	var d = true;
	str = str.substring(0,1).toLowerCase() + str.substring(1, str.length);
	while(d){
		var i = str.indexOf(" ");
		if(i<0)	d = false;
		else{				
			var temp1 = str.substring(0 , i);
			var temp2 = str.substring(i+2, str.length);
			str = temp1 + str.substring(i+1,i+2).toUpperCase() + temp2;
		}	
	}
	return str;
};
/* -------------------------------------------------------------------------------------
	Replace space with underscore: low tide -> low_tide
   ========================================================= */
qDojo.core.underscore = function(/*String*/ str)
{
	// summary: replaces all " " with "_"
	return str.replace(/ /g,'_');
};
/* -------------------------------------------------------------------------------------
	Change string to sentence case: low tide -> Low Tide
   ========================================================= */
qDojo.core.toSentenceCase = function(/*String*/ str)
{
	// summary: converts first letters of word to upper case
	var temp = str.split(" ");
	var out = "";	
	for(var i = 0 ; i < temp.length ; i++){
		out += String.toUpperCase(temp[i].charAt(0)) + temp[i].substring(1) + " ";
	}
	return out;	// String
};
/* -------------------------------------------------------------------------------------
	Search occurence, start from last character
   ========================================================= */
qDojo.core.searchLast = function(/*String*/ str , value)
{
	// summary: finds the occurrence of "value" in "str" starting from the end
	var pos = [];
	while(1){
		var i = pos.length;
		pos[i] = str.search(value);		
		if( pos[i] > -1 )	str = str.substr(pos[i]+1);
		else	break;				
	}	
	var ret = 0
	for(var i=0; i < pos.length ; i++){
		ret += pos[i];
		if(i>0) ret=ret+1;
	}
	return ret;		
};